/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.general.bc;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.cip.esymons.colegiatura.bc.ColegiadoFacadeLocal;
import org.cip.esymons.colegiatura.be.Colegiado;
import org.cip.esymons.general.be.PersonaNatural;

/**
 *
 * @author OscarA
 */
public class PersonaNaturalDelegate {

    PersonaNaturalFacadeLocal local;
    ColegiadoFacadeLocal localColegiado;

    public PersonaNaturalDelegate() {
        local = lookupPersonaNaturalFacade();
        localColegiado = lookupColegiadoFacade();
    }

    public void actualizar(PersonaNatural persona) throws Exception{
        local.edit(persona);
    }

    public PersonaNatural buscarXDNI(String dni) throws Exception{
        return local.buscarXDni1(dni);
    }

    public PersonaNatural buscarxId(Long id) {
        try {
            return local.find(id);
        } catch (Exception e) {
            return new PersonaNatural();
        }
    }

    public void registrar(PersonaNatural per) {
        try {
            local.create(per);
        } catch (Exception e) {
            System.out.println("ERROR ingreso persona natural " + e.toString());
        }
    }

    public List<PersonaNatural> listarAsistencia() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupPersonaNaturalFacade();
            throw new Exception(e.getStackTrace().toString(), e);
        }
    }

    public List<PersonaNatural> listarPersonas() throws Exception {
        try {
            return local.listarPersonas();
        } catch (Exception e) {
            local = lookupPersonaNaturalFacade();
            throw new Exception(e.getStackTrace().toString(), e);
        }
    }

    private PersonaNaturalFacadeLocal lookupPersonaNaturalFacade() {
        try {
            Context c = new InitialContext();
            return (PersonaNaturalFacadeLocal) c.lookup("java:comp/env/PersonaNaturalFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    /*
     *  Metodos de Colegiatura
     */
     public Colegiado buscarPersonaXNroCip(String nroCIP) {
         return localColegiado.buscarPersonaXNroCip(nroCIP);
     }

     public PersonaNatural buscarPersonaXDni(String nroDNI) {
         return localColegiado.buscarPersonaXDni(nroDNI);
     }

     //lizardo estuvo aqui
     public PersonaNatural buscarDNI(String dni) throws Exception {
         return local.buscarXDni(dni);
     }


    private ColegiadoFacadeLocal lookupColegiadoFacade() {
        try {
            Context c = new InitialContext();
            return (ColegiadoFacadeLocal) c.lookup("java:comp/env/ColegiadoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }


    
}
